class FieldValidator {
  constructor(txtId, validatorFunction) {
    this.input = $("#" + txtId);
    this.p = this.input.nextElementSibling;
    this.validatorFunction = validatorFunction;

    this.input.onblur = () => {
      this.validate();
    };
  }

  static async validate(...validators) {
    const ret = await Promise.all(validators.map((v) => v.validate()));
    return ret.every((r) => r);
  }

  async validate() {
    const err = await this.validatorFunction(this.input.value);
    if (err) {
      this.p.innerText = err;
      return false;
    } else {
      this.p.innerText = "";
      return true;
    }
  }
}
